package com.codegenerator.util;

import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;

/**
 * 文件处理工具类
 */
public class FileUtils {
    
    /**
     * 获取输出文件路径
     */
    public static String getOutputFilePath(String baseDir, String packageName, String fileName) {
        // 将包名转换为目录结构
        String packageDir = packageName.replace(".", File.separator);
        return baseDir + File.separator + packageDir + File.separator + fileName;
    }
    
    /**
     * 创建目录
     */
    public static void createDirectory(File directory) throws IOException {
        if (!directory.exists()) {
            if (!directory.mkdirs()) {
                throw new IOException("无法创建目录: " + directory.getAbsolutePath());
            }
        }
    }
    
    /**
     * 下划线转驼峰命名法
     * @param str 源字符串
     * @param firstCharUpper 首字母是否大写
     */
    public static String underlineToCamel(String str, boolean firstCharUpper) {
        if (StringUtils.isBlank(str)) {
            return str;
        }
        
        StringBuilder result = new StringBuilder();
        boolean nextUpperCase = false;
        
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            
            if (c == '_') {
                nextUpperCase = true;
            } else {
                if (nextUpperCase) {
                    result.append(Character.toUpperCase(c));
                    nextUpperCase = false;
                } else {
                    result.append(Character.toLowerCase(c));
                }
            }
        }
        
        // 处理首字母大小写
        if (result.length() > 0) {
            if (firstCharUpper) {
                result.setCharAt(0, Character.toUpperCase(result.charAt(0)));
            } else {
                result.setCharAt(0, Character.toLowerCase(result.charAt(0)));
            }
        }
        
        return result.toString();
    }
}
